package com.tbit.uqbike.client.controller;

import com.tbit.common.constant.CommonConstant;
import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.QuickReply;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.QuickReplyService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 快捷回复表(QuickReply)表控制层
 *
 * @author shanyong.mo
 * @since 2025-02-11 10:09:05
 */
@RestController
@RequestMapping("/quickReply")
public class QuickReplyController {
    /**
     * 服务对象
     */
    @Resource
    private QuickReplyService quickReplyService;
    @Autowired
    private TokenService tokenService;

    /**
     * 分页查询
     * @param pageNum 页码
     * @param pageSize 页长
     * @param search 搜索内容
     * @return 查询结果
     */
    @GetMapping("/queryByPage")
    public void queryByPage(String token, Integer pageNum, Integer pageSize, String search, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        try {
            User user = tokenService.getUser(token);
            QuickReply query = new QuickReply();
            query.setPlatform(CommonConstant.QUICK_REPLY_PLATFORM_CLIENT);
            query.setAccountId(user.getAccountId());
            query.setUserId(user.getUserId());
            List<QuickReply> quickReplyList = this.quickReplyService.queryByPage(query, pageNum, pageSize, search);
            Integer count = quickReplyService.queryCount(query, search);
            map.put("ret",1);
            map.put("data",quickReplyList);
            map.put("total",count);
        }catch (BaseException e){
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);

    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public void queryById(@PathVariable("id") Integer id,HttpServletRequest request,HttpServletResponse response) {
        QuickReply quickReply = this.quickReplyService.queryById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("data",quickReply);
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 新增数据
     *
     * @param quickReply 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public void add(QuickReply quickReply,String token,HttpServletRequest request,HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        try {
            Assert.notNullCheckLength(quickReply.getQuickReply(),CommonConstant.QUICK_REPLY_MAX_LENGTH,"快捷回复内容为空或超过指定长度200字符");
            User user = tokenService.getUser(token);
            quickReply.setPlatform(CommonConstant.QUICK_REPLY_PLATFORM_CLIENT);
            quickReply.setAccountId(user.getAccountId());
            quickReply.setUserId(user.getUserId());
            quickReply = this.quickReplyService.insertCheckNull(quickReply);
            map.put("ret",1);
            map.put("data",quickReply);
        }catch (BaseException e){
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);

    }

    /**
     * 编辑数据
     *
     * @param quickReply 实体
     * @return 编辑结果
     */
    @PutMapping("/edit")
    public void edit(QuickReply quickReply,HttpServletRequest request,HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        try {
            Assert.notNullCheckLength(quickReply.getQuickReply(),CommonConstant.QUICK_REPLY_MAX_LENGTH,"快捷回复内容为空或超过指定长度200字符");
            quickReply = this.quickReplyService.updateCheckNull(quickReply);
            map.put("ret",1);
            map.put("data",quickReply);
        }catch (BaseException e){
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    public void deleteById(Integer id,HttpServletResponse response,HttpServletRequest request) {
        boolean b = this.quickReplyService.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("ret",b ? 1 : 0);
        OutputUtil.renderText(request, response, map);
    }

}

